package com.ruoyi.basebill.task;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.impl.OmsSaleissuebillServiceImpl;
import com.ruoyi.basebill.service.impl.OmsSumsaleissuebillServiceImpl;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Component("OmsSumSaleIssueBillTask")
public class OmsSumSaleIssueBillTask {


    //注入自己
    @Autowired
    private OmsSumSaleIssueBillTask sumSaleIssueBillTask;

    //销售出库单服务层接口
    @Autowired
    private OmsSaleissuebillServiceImpl saleissuebillService;

    //单据自动任务工具类
    @Autowired
    private OmsTaskUtil omsTaskUtil;

    //汇总销出单服务层接口
    @Autowired
    private OmsSumsaleissuebillServiceImpl sumsaleissuebillService;

    //汇总销出单服务层接口
    @Autowired
    private DismantlingBillTask dismantlingBillTask;


    /**
     * 查出符合条件的所有销出单，汇总成汇总销出单
     */
    @Transactional
    public void summary(){
        dismantlingBillTask.disBill();
        List<OmsSaleissuebill> omsSaleissuebills = saleissuebillService.selectSumBillNumberIsNull();
        if (StringUtils.isEmpty(omsSaleissuebills)){
            return;
        }
        //准备一个集合，存储汇总销出单
        ArrayList<OmsSumsaleissuebill> omsSumsaleissuebills = new ArrayList<>();
        //准备一个Map，存储汇总销出单编码和对应的被汇总的销出单id，用于后续进行反写字段
        HashMap<String,ArrayList<Long>> sumBillNumberAndIds = new HashMap<>();
        sumSaleIssueBillTask.summarySaleissuebill(omsSaleissuebills,omsSumsaleissuebills,sumBillNumberAndIds);
        //方法处理完，集合中存储了汇总完毕的汇总销出单，以及汇总销出单编码以及对应的原单id
        for (OmsSumsaleissuebill omsSumsaleissuebill : omsSumsaleissuebills) {
            //创建一个新的分录，用来存储自己和自己汇总完的数据
            ArrayList<OmsSumsaleissuebillentry> omsSumsaleissuebillentries = new ArrayList<>();
            //遍历原来的分录，合并相同的数据
            ForA:
            for (OmsSumsaleissuebillentry omsSumsaleissuebillentry : omsSumsaleissuebill.getOmsSumsaleissuebillentryList()) {
                for (int i = 0; i < omsSumsaleissuebillentries.size(); i++) {
                    //合并完自己之后的分录数据
                    OmsSumsaleissuebillentry omsSumsaleissuebillentry1 = omsSumsaleissuebillentries.get(i);
                    boolean b = judgeSumBillEntry(omsSumsaleissuebillentry, omsSumsaleissuebillentry1);
                    if (b){
                        summarySumBillEntry(omsSumsaleissuebillentry,omsSumsaleissuebillentry1);
                        omsSumsaleissuebillentries.set(i,omsSumsaleissuebillentry1);
                        continue ForA;
                    }
                }
                omsSumsaleissuebillentries.add(omsSumsaleissuebillentry);
            }
            omsSumsaleissuebill.setOmsSumsaleissuebillentryList(omsSumsaleissuebillentries);
            sumsaleissuebillService.insertOmsSumsaleissuebill(omsSumsaleissuebill);
        }
        for (String s : sumBillNumberAndIds.keySet()) {
            saleissuebillService.updateSumBillNumberByIds(s,sumBillNumberAndIds.get(s));
        }

    }

    /**
     * 传入销售出库单集合，获取汇总单集合以及对应的反写字段数据
     * @param omsSaleissuebills 销售出库单集合
     * @param omsSumsaleissuebills 汇总销售出库单集合
     * @param sumBillNumberAndIds 汇总生成的汇总单编码和对应的原单id
     */
    public void summarySaleissuebill(List<OmsSaleissuebill> omsSaleissuebills,ArrayList<OmsSumsaleissuebill> omsSumsaleissuebills,HashMap<String,ArrayList<Long>> sumBillNumberAndIds){
        //准备一个集合用来存储生成的所有单据编号
        ArrayList<String> billNumbers = new ArrayList<>();

        //如果集合不为null且不为空
        //遍历集合，将id存储在hashset中，用于后续进行批量查询。
        HashSet<Long> idSet = new HashSet<Long>();
        HashMap<Long, OmsSaleissuebill> saleMap = new HashMap<Long, OmsSaleissuebill>();
        if(omsSaleissuebills!=null&& !omsSaleissuebills.isEmpty()) {
            for (int i = 0; i < omsSaleissuebills.size(); i++) {
                OmsSaleissuebill omsSaleissuebill = omsSaleissuebills.get(i);
                Long id = omsSaleissuebill.getId();
                idSet.add(id);
            }
            //根据id集合进行批量查询
            List<OmsSaleissuebill> omsSaleissuebillsList = saleissuebillService.selectOmsSaleissuebillByIds(idSet);
            //循环集合，将数据存储在map中，用于后续快速获取。
            if(omsSaleissuebillsList!=null&& !omsSaleissuebillsList.isEmpty()) {
                for (int i = 0; i < omsSaleissuebillsList.size(); i++) {
                    OmsSaleissuebill omsSaleissuebill = omsSaleissuebillsList.get(i);
                    Long id = omsSaleissuebill.getId();
                    saleMap.put(id, omsSaleissuebill);
                }
            }
        }
        for (int i = 0; i < omsSaleissuebills.size(); i++) {
            OmsSaleissuebill omsSaleissuebill1 = omsSaleissuebills.get(i);
            omsSaleissuebill1 = saleMap.get(omsSaleissuebill1.getId());
            String summaryBillNumber = sumSaleIssueBillTask.summarySaleBill(omsSaleissuebill1, omsSumsaleissuebills,billNumbers);
            Long id = omsSaleissuebill1.getId();
            if (sumBillNumberAndIds.containsKey(summaryBillNumber)){
                ArrayList<Long> tempIds = sumBillNumberAndIds.get(summaryBillNumber);
                tempIds.add(id);
                sumBillNumberAndIds.put(summaryBillNumber,tempIds);
            }else {
                ArrayList<Long> tempIdList = new ArrayList<>();
                tempIdList.add(id);
                sumBillNumberAndIds.put(summaryBillNumber,tempIdList);
                billNumbers.add(summaryBillNumber);
            }
        }

    }
    public String summarySaleBill(OmsSaleissuebill omsSaleissuebill,ArrayList<OmsSumsaleissuebill> omsSumsaleissuebills,ArrayList<String> billNumbers) {
        //1.查找匹配的汇总单据
        int matchIndex = -1;
        for (int i = 0; i < omsSumsaleissuebills.size(); i++) {
            OmsSumsaleissuebill omsSumsaleissuebill = omsSumsaleissuebills.get(i);
            boolean b = sumSaleIssueBillTask.judgeBillHead(omsSaleissuebill, omsSumsaleissuebill);
            if (b){
                matchIndex = i;
                break;
            }
            }
        //2.如果没有匹配的汇总单据，则创建一个新的汇总单据
        if (matchIndex == -1){
            OmsSumsaleissuebill omsSumsaleissuebill = new OmsSumsaleissuebill();
            sumSaleIssueBillTask.initializeOneBill(omsSaleissuebill,omsSumsaleissuebill,billNumbers);
            //为新的汇总单据创建条目
            List<OmsSumsaleissuebillentry> newEntries = new ArrayList<>();
            for (OmsSaleissuebillentry omsSaleissuebillentry : omsSaleissuebill.getOmsSaleissuebillentryList()) {
                OmsSumsaleissuebillentry omsSumsaleissuebillentry1 = new OmsSumsaleissuebillentry();
                sumSaleIssueBillTask.initializeOneBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry1);
                newEntries.add(omsSumsaleissuebillentry1);
            }
            omsSumsaleissuebill.setOmsSumsaleissuebillentryList(newEntries);
            omsSumsaleissuebills.add(omsSumsaleissuebill);
            return omsSumsaleissuebill.getBillnumber();
        }
        //3.如果有匹配的汇总单据，则将当前销出单添加到匹配的汇总单据中
        OmsSumsaleissuebill omsSumsaleissuebill1 = omsSumsaleissuebills.get(matchIndex);
        List<OmsSumsaleissuebillentry> omsSumsaleissuebillentryList = omsSumsaleissuebill1.getOmsSumsaleissuebillentryList();

        //4.创建查找表，用于快速进行匹配。
        Map<String, Integer> entryIndexMap = createEntryIndexMap(omsSumsaleissuebillentryList);
        //开始便利原数据分录
        for (OmsSaleissuebillentry omsSaleissuebillentry : omsSaleissuebill.getOmsSaleissuebillentryList()) {
            String key = generateEntryKey(omsSaleissuebillentry);
            if (entryIndexMap.containsKey(key)){
                int index = entryIndexMap.get(key);
                OmsSumsaleissuebillentry omsSumsaleissuebillentry1 = omsSumsaleissuebillentryList.get(index);
                sumSaleIssueBillTask.summaryBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry1);
                omsSumsaleissuebillentryList.set(index,omsSumsaleissuebillentry1);
            }else {
                OmsSumsaleissuebillentry omsSumsaleissuebillentry1 = new OmsSumsaleissuebillentry();
                sumSaleIssueBillTask.initializeOneBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry1);
                omsSumsaleissuebillentryList.add(omsSumsaleissuebillentry1);
                //更新索引表
                entryIndexMap.put(key, omsSumsaleissuebillentryList.size() - 1);
            }
        }
        return omsSumsaleissuebill1.getBillnumber();
    }




    private Map<String, Integer> createEntryIndexMap(List<OmsSumsaleissuebillentry> entries) {
        Map<String, Integer> indexMap = new HashMap<>();
        for (int i = 0; i < entries.size(); i++) {
            String key = generateEntryKey(entries.get(i));
            indexMap.put(key, i);
        }
        return indexMap;
    }

    private String generateEntryKey(OmsSumsaleissuebillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String warehousenumber = entry.getWarehousenumber();
        String materialnumber = entry.getMaterialnumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        BigDecimal price = entry.getPrice();
        BigDecimal taxrate = entry.getTaxrate();
        BigDecimal systaxprice = entry.getSystaxprice();
        BigDecimal taxprice = entry.getTaxprice();
        Long ispresent = entry.getIspresent();
        return warehousenumber + ':' + materialnumber + ':' + unitnumber + ':' + baseunitnumber + ':' + price + ':' + taxrate + ':' + systaxprice + ':' + taxprice + ':' + ispresent;
        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }

    private String generateEntryKey(OmsSaleissuebillentry entry) {
        // 根据您的业务逻辑生成唯一键
        // 这里假设使用某些属性组合作为键，例如：
        String warehousenumber = entry.getWarehousenumber();
        String materialnumber = entry.getMaterialnumber();
        String unitnumber = entry.getUnitnumber();
        String baseunitnumber = entry.getBaseunitnumber();
        BigDecimal price = entry.getPrice();
        BigDecimal taxrate = entry.getTaxrate();
        BigDecimal systaxprice = entry.getSystaxprice();
        BigDecimal taxprice = entry.getTaxprice();
        Long ispresent = entry.getIspresent();
        return warehousenumber + ':' + materialnumber + ':' + unitnumber + ':' + baseunitnumber + ':' + price + ':' + taxrate + ':' + systaxprice + ':' + taxprice + ':' + ispresent;
        // 注意：请根据实际情况调整这个键的生成逻辑，使用真正能唯一标识条目的属性组合
    }


        /**
         * 将销出单汇总到汇总销出单中
         * @param omsSaleissuebill 销出单
         * @param omsSumsaleissuebills 汇总销出单集合
         * @param billNumbers 存储了单据编号的集合
         * @return 汇总销出单的单据编码
         */
    public String summarySaleBill2(OmsSaleissuebill omsSaleissuebill,ArrayList<OmsSumsaleissuebill> omsSumsaleissuebills,ArrayList<String> billNumbers){



        //循环汇总销出单集合
        for (int i = 0; i < omsSumsaleissuebills.size(); i++){
            OmsSumsaleissuebill omsSumsaleissuebill = omsSumsaleissuebills.get(i);
            //判断当前传入的销出单和汇总销出单的单据头是否相同
            boolean b = sumSaleIssueBillTask.judgeBillHead(omsSaleissuebill, omsSumsaleissuebill);
            if (b){
                List<OmsSaleissuebillentry> omsSaleissuebillentryList = omsSaleissuebill.getOmsSaleissuebillentryList();
                ForA :
                //遍历销出单分录
                for (OmsSaleissuebillentry omsSaleissuebillentry : omsSaleissuebillentryList) {
                    List<OmsSumsaleissuebillentry> omsSumsaleissuebillentryList = omsSumsaleissuebill.getOmsSumsaleissuebillentryList();
                    ForB :
                    //遍历汇总销出单分录
                    for (int j = 0; j < omsSumsaleissuebillentryList.size(); j++) {
                        OmsSumsaleissuebillentry omsSumsaleissuebillentry = omsSumsaleissuebillentryList.get(j);
                        boolean b1 = sumSaleIssueBillTask.judgeBillEntry(omsSaleissuebillentry, omsSumsaleissuebillentry);
                        if (b1){
                            sumSaleIssueBillTask.summaryBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry);
                            omsSumsaleissuebillentryList.set(j,omsSumsaleissuebillentry);
                            continue ForA;
                        }
                    }
                    OmsSumsaleissuebillentry omsSumsaleissuebillentry = new OmsSumsaleissuebillentry();
                    sumSaleIssueBillTask.initializeOneBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry);
                    omsSumsaleissuebillentryList.add(omsSumsaleissuebillentry);
                    omsSumsaleissuebill.setOmsSumsaleissuebillentryList(omsSumsaleissuebillentryList);
                }
                omsSumsaleissuebills.set(i,omsSumsaleissuebill);
                return omsSumsaleissuebill.getBillnumber();
            }

        }
        OmsSumsaleissuebill omsSumsaleissuebill = new OmsSumsaleissuebill();
        sumSaleIssueBillTask.initializeOneBill(omsSaleissuebill,omsSumsaleissuebill,billNumbers);
        omsSumsaleissuebills.add(omsSumsaleissuebill);
        return omsSumsaleissuebill.getBillnumber();
    }

    /**
     * 判断销出单单据头和汇总销出单单据头是否可以合并
     * @param omsSaleissuebill 销出单
     * @param omsSumsaleissuebill 汇总销出单
     * @return true：可以合并。false：不能合并
     */
    public boolean judgeBillHead(OmsSaleissuebill omsSaleissuebill,OmsSumsaleissuebill omsSumsaleissuebill){
        //获取销出单单据头数据
        Date bizdate = omsSaleissuebill.getBizdate();
        String storageorgnumber = omsSaleissuebill.getStorageorgnumber();
        String biztypenumber = omsSaleissuebill.getBiztypenumber();
        String transactiontypenumber = omsSaleissuebill.getTransactiontypenumber();
        String customernumber = omsSaleissuebill.getCustomernumber();
        String accountcompanynumber = omsSaleissuebill.getAccountcompanynumber();
        String costcenternumber = omsSaleissuebill.getCostcenternumber();
        String currency = omsSaleissuebill.getCurrency();
        BigDecimal exchangerate = omsSaleissuebill.getExchangerate();
        String srcbilltype = omsSaleissuebill.getSrcbilltype();
        //来源系统
        String srcsys = omsSaleissuebill.getSrcsys();

        //获取汇总销出单单据头数据
        Date bizdate1 = omsSumsaleissuebill.getBizdate();
        String storageorgnumber1 = omsSumsaleissuebill.getStorageorgnumber();
        String biztypenumber1 = omsSumsaleissuebill.getBiztypenumber();
        String transactiontypenumber1 = omsSumsaleissuebill.getTransactiontypenumber();
        String customernumber1 = omsSumsaleissuebill.getCustomernumber();
        String accountcompanynumber1 = omsSumsaleissuebill.getAccountcompanynumber();
        String costcenternumber1 = omsSumsaleissuebill.getCostcenternumber();
        String currency1 = omsSumsaleissuebill.getCurrency();
        BigDecimal exchangerate1 = omsSumsaleissuebill.getExchangerate();
        String description = omsSumsaleissuebill.getDescription();
        String srcsys1 = omsSumsaleissuebill.getSrcsys();

        //单据类型
        boolean boolean10 = OmsTaskUtil.juedeString(srcbilltype, description);
        if (!boolean10){
            return false;
        }
        //业务日期
        boolean boolean1 = OmsTaskUtil.juedeDate(bizdate,bizdate1);
        //库存组织编码
        boolean boolean2 = OmsTaskUtil.juedeString(storageorgnumber,storageorgnumber1);
        //业务类型编码
        boolean boolean3 = OmsTaskUtil.juedeString(biztypenumber,biztypenumber1);
        //事务类型编码
        boolean boolean4 = OmsTaskUtil.juedeString(transactiontypenumber,transactiontypenumber1);
        //送货客户编码
        boolean boolean5 = OmsTaskUtil.juedeString(customernumber,customernumber1);
        //记账公司编码
        boolean boolean6 = OmsTaskUtil.juedeString(accountcompanynumber,accountcompanynumber1);
        //成本中心编码
        boolean boolean7 = OmsTaskUtil.juedeString(costcenternumber,costcenternumber1);
        //币别
        boolean boolean8 = OmsTaskUtil.juedeString(currency,currency1);
        //汇率
        boolean boolean9 = OmsTaskUtil.juedeBigDecimal(exchangerate,exchangerate1);
        //来源系统
        boolean boolean11 = OmsTaskUtil.juedeString(srcsys, srcsys1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8&&boolean9&&boolean11;
    }

    /**
     * 判断销出单和汇总销出单的分录是否可以合并
     * @param saleissuebillentry 销出单分录
     * @param sumsaleissuebillentry 汇总销出单分录
     * @return true：可以合并。false：不能合并
     */
    public boolean judgeBillEntry(OmsSaleissuebillentry saleissuebillentry,OmsSumsaleissuebillentry sumsaleissuebillentry){
        String warehousenumber = saleissuebillentry.getWarehousenumber();
        String materialnumber = saleissuebillentry.getMaterialnumber();
        String unitnumber = saleissuebillentry.getUnitnumber();
        String baseunitnumber = saleissuebillentry.getBaseunitnumber();
        BigDecimal price = saleissuebillentry.getPrice();
        BigDecimal taxrate = saleissuebillentry.getTaxrate();
        BigDecimal systaxprice = saleissuebillentry.getSystaxprice();
        BigDecimal taxprice = saleissuebillentry.getTaxprice();
        Long ispresent = saleissuebillentry.getIspresent();

        String warehousenumber1 = sumsaleissuebillentry.getWarehousenumber();
        String materialnumber1 = sumsaleissuebillentry.getMaterialnumber();
        String unitnumber1 = sumsaleissuebillentry.getUnitnumber();
        String baseunitnumber1 = sumsaleissuebillentry.getBaseunitnumber();
        BigDecimal price1 = sumsaleissuebillentry.getPrice();
        BigDecimal taxrate1 = sumsaleissuebillentry.getTaxrate();
        BigDecimal systaxprice1 = sumsaleissuebillentry.getSystaxprice();
        BigDecimal taxprice1 = sumsaleissuebillentry.getTaxprice();
        Long ispresent1 = sumsaleissuebillentry.getIspresent();

        boolean boolean1 = warehousenumber.equals(warehousenumber1);
        boolean boolean2 = materialnumber.equals(materialnumber1);
        boolean boolean3 = unitnumber.equals(unitnumber1);
        boolean boolean4 = baseunitnumber.equals(baseunitnumber1);
        boolean boolean5 = price.equals(price1);
        boolean boolean6 = taxrate.equals(taxrate1);
        boolean boolean7 = taxprice.equals(taxprice1);
        boolean boolean8 = ispresent.equals(ispresent1);
        //新增系统含税单价的判断
        boolean boolean9 = OmsTaskUtil.juedeBigDecimal(systaxprice,systaxprice1);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8&&boolean9;
    }

    /**
     * 判断汇总分录和汇总分录是否可以合并
     * @param omsSumsaleissuebillentry1
     * @param omsSumsaleissuebillentry2
     * @return
     */
    public boolean  judgeSumBillEntry(OmsSumsaleissuebillentry omsSumsaleissuebillentry1,OmsSumsaleissuebillentry omsSumsaleissuebillentry2){
        String warehousenumber1 = omsSumsaleissuebillentry1.getWarehousenumber();
        String materialnumber1 = omsSumsaleissuebillentry1.getMaterialnumber();
        String unitnumber1 = omsSumsaleissuebillentry1.getUnitnumber();
        String baseunitnumber1 = omsSumsaleissuebillentry1.getBaseunitnumber();
        BigDecimal price1 = omsSumsaleissuebillentry1.getPrice();
        BigDecimal taxrate1 = omsSumsaleissuebillentry1.getTaxrate();
        BigDecimal systaxprice1 = omsSumsaleissuebillentry1.getSystaxprice();
        BigDecimal taxprice1 = omsSumsaleissuebillentry1.getTaxprice();
        Long ispresent1 = omsSumsaleissuebillentry1.getIspresent();

        String warehousenumber2 = omsSumsaleissuebillentry2.getWarehousenumber();
        String materialnumber2 = omsSumsaleissuebillentry2.getMaterialnumber();
        String unitnumber2 = omsSumsaleissuebillentry2.getUnitnumber();
        String baseunitnumber2 = omsSumsaleissuebillentry2.getBaseunitnumber();
        BigDecimal price2 = omsSumsaleissuebillentry2.getPrice();
        BigDecimal taxrate2 = omsSumsaleissuebillentry2.getTaxrate();
        BigDecimal systaxprice2 = omsSumsaleissuebillentry2.getSystaxprice();
        BigDecimal taxprice2 = omsSumsaleissuebillentry2.getTaxprice();
        Long ispresent2 = omsSumsaleissuebillentry2.getIspresent();

        boolean boolean1 = warehousenumber1.equals(warehousenumber2);
        boolean boolean2 = materialnumber1.equals(materialnumber2);
        boolean boolean3 = unitnumber1.equals(unitnumber2);
        boolean boolean4 = baseunitnumber1.equals(baseunitnumber2);
        boolean boolean5 = price1.equals(price2);
        boolean boolean6 = taxrate1.equals(taxrate2);
        boolean boolean7 = taxprice1.equals(taxprice2);
        boolean boolean8 = ispresent1.equals(ispresent2);
        //新增判断系统含税单价
        boolean boolean9 = OmsTaskUtil.juedeBigDecimal(systaxprice1,systaxprice2);

        return boolean1&&boolean2&&boolean3&&boolean4&&boolean5&&boolean6&&boolean7&&boolean8&&boolean9;

    }

    /**
     * 合并销出单和汇总销出单分录数据
     * @param omsSaleissuebillentry 销出单分录数据
     * @param omsSumsaleissuebillentry 汇总销出单分录数据
     */
    public void summaryBillEntry(OmsSaleissuebillentry omsSaleissuebillentry,OmsSumsaleissuebillentry omsSumsaleissuebillentry){
        BigDecimal qty = omsSaleissuebillentry.getQty();
        BigDecimal baseqty = omsSaleissuebillentry.getBaseqty();
        BigDecimal amount = omsSaleissuebillentry.getAmount();
        BigDecimal tax = omsSaleissuebillentry.getTax();
        BigDecimal taxamount = omsSaleissuebillentry.getTaxamount();

        omsSumsaleissuebillentry.setQty(omsSumsaleissuebillentry.getQty().add(qty));
        omsSumsaleissuebillentry.setBaseqty(omsSumsaleissuebillentry.getBaseqty().add(baseqty));
        omsSumsaleissuebillentry.setAmount(omsSumsaleissuebillentry.getAmount().add(amount));
        omsSumsaleissuebillentry.setTax(omsSumsaleissuebillentry.getTax().add(tax));
        omsSumsaleissuebillentry.setTaxamount(omsSumsaleissuebillentry.getTaxamount().add(taxamount));
    }

    /**
     * 合并汇总分录单与汇总分录单
     * @param omsSumsaleissuebillentry1
     * @param omsSumsaleissuebillentry2
     */
    public void summarySumBillEntry(OmsSumsaleissuebillentry omsSumsaleissuebillentry1,OmsSumsaleissuebillentry omsSumsaleissuebillentry2){
        BigDecimal qty = omsSumsaleissuebillentry1.getQty();
        BigDecimal baseqty = omsSumsaleissuebillentry1.getBaseqty();
        BigDecimal amount = omsSumsaleissuebillentry1.getAmount();
        BigDecimal tax = omsSumsaleissuebillentry1.getTax();
        BigDecimal taxamount = omsSumsaleissuebillentry1.getTaxamount();

        omsSumsaleissuebillentry2.setQty(omsSumsaleissuebillentry2.getQty().add(qty));
        omsSumsaleissuebillentry2.setBaseqty(omsSumsaleissuebillentry2.getBaseqty().add(baseqty));
        omsSumsaleissuebillentry2.setAmount(omsSumsaleissuebillentry2.getAmount().add(amount));
        omsSumsaleissuebillentry2.setTax(omsSumsaleissuebillentry2.getTax().add(tax));
        omsSumsaleissuebillentry2.setTaxamount(omsSumsaleissuebillentry2.getTaxamount().add(taxamount));

    }



        /**
         * 传入销出单对象，和汇总销出单对象，完成初始化
         * @param omsSaleissuebill 销出单
         * @param omsSumsaleissuebill 汇总销出单
         * @param billNumbers 存储了所有生成的汇总单据的编码
         */
    public void initializeOneBill(OmsSaleissuebill omsSaleissuebill, OmsSumsaleissuebill omsSumsaleissuebill, ArrayList<String> billNumbers){
        //获取销出单单据头数据
        Date bizdate = omsSaleissuebill.getBizdate();
        String storageorgnumber = omsSaleissuebill.getStorageorgnumber();
        String storageorgname = omsSaleissuebill.getStorageorgname();
        String biztypenumber = omsSaleissuebill.getBiztypenumber();
        String biztypename = omsSaleissuebill.getBiztypename();
        String transactiontypenumber = omsSaleissuebill.getTransactiontypenumber();
        String transactiontypename = omsSaleissuebill.getTransactiontypename();
        String customernumber = omsSaleissuebill.getCustomernumber();
        String customername = omsSaleissuebill.getCustomername();
        String accountcompanynumber = omsSaleissuebill.getAccountcompanynumber();
        String accountcompanyname = omsSaleissuebill.getAccountcompanyname();
        String costcenternumber = omsSaleissuebill.getCostcenternumber();
        String costcentername = omsSaleissuebill.getCostcentername();
        String currency = omsSaleissuebill.getCurrency();
        BigDecimal exchangerate = omsSaleissuebill.getExchangerate();
        String srcbilltype = omsSaleissuebill.getSrcbilltype();
        //来源系统
        String srcsys = omsSaleissuebill.getSrcsys();

        //设置汇总销出单单据头数据
        omsSumsaleissuebill.setBizdate(bizdate);
        //获取单据编号之前需要判断单据编码是否存在
        String nextBillNumber = sumSaleIssueBillTask.getNextBillNumber(omsSumsaleissuebill, billNumbers);
        omsSumsaleissuebill.setBillnumber(nextBillNumber);
        omsSumsaleissuebill.setStorageorgnumber(storageorgnumber);
        omsSumsaleissuebill.setStorageorgname(storageorgname);
        omsSumsaleissuebill.setBiztypenumber(biztypenumber);
        omsSumsaleissuebill.setBiztypename(biztypename);
        omsSumsaleissuebill.setTransactiontypenumber(transactiontypenumber);
        omsSumsaleissuebill.setTransactiontypename(transactiontypename);
        omsSumsaleissuebill.setCustomernumber(customernumber);
        omsSumsaleissuebill.setCustomername(customername);
        omsSumsaleissuebill.setAccountcompanynumber(accountcompanynumber);
        omsSumsaleissuebill.setAccountcompanyname(accountcompanyname);
        omsSumsaleissuebill.setCostcenternumber(costcenternumber);
        omsSumsaleissuebill.setCostcentername(costcentername);
        omsSumsaleissuebill.setCurrency(currency);
        omsSumsaleissuebill.setExchangerate(exchangerate);
        omsSumsaleissuebill.setDescription(srcbilltype);
        omsSumsaleissuebill.setCreatetime(new Date());
        omsSumsaleissuebill.setIstrans("0");
        //来源系统
        omsSumsaleissuebill.setSrcsys(srcsys);

        List<OmsSaleissuebillentry> omsSaleissuebillentryList = omsSaleissuebill.getOmsSaleissuebillentryList();
        List<OmsSumsaleissuebillentry> omsSumsaleissuebillentries = new ArrayList<>();
        if (StringUtils.isEmpty(omsSaleissuebillentryList)){
            omsSumsaleissuebill.setOmsSumsaleissuebillentryList(omsSumsaleissuebillentries);
            return;
        }
        for (OmsSaleissuebillentry omsSaleissuebillentry : omsSaleissuebillentryList) {
            OmsSumsaleissuebillentry omsSumsaleissuebillentry = new OmsSumsaleissuebillentry();
            sumSaleIssueBillTask.initializeOneBillEntry(omsSaleissuebillentry,omsSumsaleissuebillentry);

            omsSumsaleissuebillentries.add(omsSumsaleissuebillentry);
        }
        omsSumsaleissuebill.setOmsSumsaleissuebillentryList(omsSumsaleissuebillentries);
    }


    /**
     * 用销出单分录数据生成一张汇总销出单分录数据
     * @param omsSaleissuebillentry 销出单分录
     * @param omsSumsaleissuebillentry 汇总销出单分录
     */
    public void initializeOneBillEntry(OmsSaleissuebillentry omsSaleissuebillentry,OmsSumsaleissuebillentry omsSumsaleissuebillentry){
        String warehousenumber = omsSaleissuebillentry.getWarehousenumber();
        String warehousename = omsSaleissuebillentry.getWarehousename();
        String materialnumber = omsSaleissuebillentry.getMaterialnumber();
        String materialname = omsSaleissuebillentry.getMaterialname();
        String unitnumber = omsSaleissuebillentry.getUnitnumber();
        String baseunitnumber = omsSaleissuebillentry.getBaseunitnumber();
        BigDecimal qty = omsSaleissuebillentry.getQty();
        BigDecimal baseqty = omsSaleissuebillentry.getBaseqty();
        BigDecimal price = omsSaleissuebillentry.getPrice();
        BigDecimal taxrate = omsSaleissuebillentry.getTaxrate();
        //系统含税单价
        BigDecimal systaxprice = omsSaleissuebillentry.getSystaxprice();
        BigDecimal taxprice = omsSaleissuebillentry.getTaxprice();
        BigDecimal amount = omsSaleissuebillentry.getAmount();
        BigDecimal tax = omsSaleissuebillentry.getTax();
        BigDecimal taxamount = omsSaleissuebillentry.getTaxamount();
        Long ispresent = omsSaleissuebillentry.getIspresent();

        omsSumsaleissuebillentry.setWarehousenumber(warehousenumber);
        omsSumsaleissuebillentry.setWarehousename(warehousename);
        omsSumsaleissuebillentry.setMaterialnumber(materialnumber);
        omsSumsaleissuebillentry.setMaterialname(materialname);
        omsSumsaleissuebillentry.setUnitnumber(unitnumber);
        omsSumsaleissuebillentry.setBaseunitnumber(baseunitnumber);
        omsSumsaleissuebillentry.setQty(qty);
        omsSumsaleissuebillentry.setBaseqty(baseqty);
        omsSumsaleissuebillentry.setPrice(price);
        omsSumsaleissuebillentry.setTaxrate(taxrate);
        //系统含税单价
        omsSumsaleissuebillentry.setSystaxprice(systaxprice);
        omsSumsaleissuebillentry.setTaxprice(taxprice);
        omsSumsaleissuebillentry.setAmount(amount);
        omsSumsaleissuebillentry.setTax(tax);
        omsSumsaleissuebillentry.setTaxamount(taxamount);
        omsSumsaleissuebillentry.setIspresent(ispresent);
    }

    /**
     * 判断对应的业务日期的单据是否存在
     * @param omsSumsaleissuebill 汇总销出单对象
     * @param billNumbers 存储的单据编码
     * @return 生成的单据编码
     */
    public String getNextBillNumber(OmsSumsaleissuebill omsSumsaleissuebill,ArrayList<String> billNumbers){
        //时间字符串产生方式
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        //组合流水号前一部分
        String billNumberPfix = format.format(omsSumsaleissuebill.getBizdate());
        if (StringUtils.isEmpty(billNumbers)){
            return omsTaskUtil.setBillNumber(omsSumsaleissuebill);
        }else {
            for (int i = billNumbers.size()-1; i >= 0; i--){
                String billNumber = billNumbers.get(i);
                if (billNumber.contains(billNumberPfix)){
                    //从单据编号中截取后6位
                    String billNumberEnd = billNumber.substring(billNumber.length() - 6);
                    int endNumber = Integer.parseInt(billNumberEnd);
                    int tempNumber = 1000000 + endNumber + 1;
                    //将首位的1去掉之后，拼接成流水号
                    billNumber = "HZXXCK" + '-' + billNumberPfix + '-' + String.valueOf(tempNumber).substring(1);
                    return billNumber;
                }
            }
            return omsTaskUtil.setBillNumber(omsSumsaleissuebill);
        }
    }
}
